﻿//-----------------------------------------------------------------------
// <copyright file="ShowNewVersionCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Windows.NewVersion
{
    using System;
    using System.Windows;

    using ChatBox.DomainObjects;
    using ChatBox.DomainObjects.XmlFeedObjects;
    using ChatBox.UI.Commands;

    using Utility;

    /// <summary>
    /// Command to show the new version
    /// </summary>
    public class ShowNewVersionCommand : GenericCommandBase<ClientVersion>
    {
        /// <summary>
        /// Exeute the command
        /// </summary>
        /// <param name="value">the client version to use</param>
        public override void ExecuteOnValue(ClientVersion value)
        {
            NewVersionViewModel viewModel = Locator.Resolve<NewVersionViewModel>();
            Populate(viewModel, value);

            NewVersionWindow newVersionWindow = Locator.Resolve<NewVersionWindow>();
            newVersionWindow.DataContext = viewModel;
            viewModel.Window = newVersionWindow;

            newVersionWindow.Loaded += (s, e) => newVersionWindow.CenterInWindow(Application.Current.MainWindow);
            newVersionWindow.Show();
        }

        /// <summary>
        /// Populate the view model from the data
        /// </summary>
        /// <param name="model">the model to populate</param>
        /// <param name="versionData">the data to use</param>
        private static void Populate(NewVersionViewModel model, ClientVersion versionData)
        {
            model.Title = "Chatbox " + VersionConstants.CurrentVersion;
            model.CurrentVersionText =
                "Version " + versionData.LatestVersion +
                " is available.\nYou have version " + VersionConstants.CurrentVersion + ".";
            
            model.DownloadLinkText = "Download Chatbox " + versionData.LatestVersion;
            model.DownloadLink = new Uri(versionData.Download);
            
            model.HomepageLinkText = "Chatbox homepage";
            model.HomepageLink = new Uri(versionData.Homepage);
        }
    }
}
